package com.example.administrator.fangbook.activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.example.administrator.fangbook.HtlMvp.activity.BaseMvpActivity;
import com.example.administrator.fangbook.R;
import com.example.administrator.fangbook.activity.adapter.MainAdapter;
import com.example.administrator.fangbook.appliction.HtlApplication;
import com.example.administrator.fangbook.dataLibrary.book.IBookbase;
import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.importlocal.ImportLocalBookActivity;
import com.example.administrator.fangbook.mvp_presenter.MainPresenter;
import com.example.administrator.fangbook.mvp_view.MainView;
import com.example.administrator.fangbook.read.ReadActivity;
import com.example.administrator.fangbook.utils.FileUtils;
import com.example.administrator.fangbook.utils.LogUtil;
import com.example.administrator.fangbook.utils.ToastUtils;

import java.sql.SQLException;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 主界面
 */
public class MainActivity extends BaseMvpActivity<MainView, MainPresenter> implements MainView {
    //    @Bind(R.id.home_sideslip)
//    RelativeLayout homeSideslip;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.add_book_button)
    Button addBookButton;
    @Bind(R.id.home_content)
    RelativeLayout homeContent;
    private boolean finish;
    private List<ShelfItemBook> shelfItemBooks;
    private MainAdapter mMainAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        initReacyclerView();
        try {
            shelfItemBooks = HtlApplication.getApplication().getmDataHelper().getShelfBookDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (shelfItemBooks != null && shelfItemBooks.size() > 0) {
            addBookButton.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            initRecyclerViewData();
        } else {
            addBookButton.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }

    }

    private void initRecyclerViewData() {
        mMainAdapter.setList(shelfItemBooks);
    }

    private void initReacyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mMainAdapter = new MainAdapter(this);
        mMainAdapter.setOnItemClickLisener(new MainAdapter.OnItemClickLisener() {
            @Override
            public void itemClick(ShelfItemBook book) {
                doReadBook(book);
            }
        });
        recyclerView.setAdapter(mMainAdapter);
    }

    /**
     * 打开书籍
     *
     * @param book
     */
    private void doReadBook(ShelfItemBook book) {
        if (book == null || TextUtils.isEmpty(book.getBookId())) {
            ToastUtils.show("检查书籍是否存在！");
            return;
        }
        if (book.getBookType() == IBookbase.BookType.Type_Txt && !FileUtils.isFileExist(book.getBookId())) {
            ToastUtils.show("检查书籍是否存在！");
            return;
        }
        ReadActivity.startThis(this,book);
    }


    @Override
    public void onBackPressed() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish = false;
            }
        }, 2000);
        if (finish) {
            moveTaskToBack(false);
        } else {
            finish = true;
            ToastUtils.show("再按一次退出");
        }
    }

    @Override
    protected boolean getIsHideState() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public MainPresenter bindMvpPresenter() {
        return new MainPresenter();
    }

    @Override
    public MainView bindMvpView() {
        return this;
    }


    @OnClick(R.id.add_book_button)
    public void onViewClicked() {
        startActivity(new Intent(this, ImportLocalBookActivity.class));
    }
}
